use std::collections::HashMap;
use chrono::Local;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug)]
pub struct AppSession {
    data: HashMap<String, String>,
}

impl AppSession {
    pub fn new() -> Self {
        Self {data: HashMap::<String, String>::new() }
    }

    pub fn get_value(&self, key: &str) -> Option<String> {
        self.data.get(key).cloned()
    }

    pub fn contains_key(&self, key: &str) -> bool {
        self.data.contains_key(key)
    }

    pub fn set_value(&mut self, key: String, value: String) {
        self.data.insert(key, value);
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocalItem {
    pub key: String,
    pub expiry: u64,
}

impl LocalItem {
    pub fn new(key: String, expiry: u64) -> Self {
        Self { key, expiry }
    }
    
    pub fn is_expired(&self) -> bool {
        self.expiry < Local::now().timestamp_millis().abs() as u64
    }
}